package com.zone.core.coroutine

import android.net.ParseException
import com.google.gson.JsonParseException
import com.zone.core.CoreApplication
import com.zone.core.R
import org.jetbrains.anko.toast
import org.json.JSONException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.net.UnknownServiceException
import javax.net.ssl.SSLHandshakeException

class ExceptionHandler {
    fun handleException(throwable: Throwable) {
        when (throwable) {
            //连接异常
            is ConnectException -> CoreApplication.coreApp.toast(R.string.http_request_exception)
            is UnknownHostException,
            is UnknownServiceException -> CoreApplication.coreApp.toast(R.string.http_server_exception)
            is SocketTimeoutException -> CoreApplication.coreApp.toast(R.string.http_timeout_exception)
            //数据解析异常
            is JsonParseException,
            is JSONException,
            is ParseException -> CoreApplication.coreApp.toast(R.string.http_data_exception)
            //证书异常
            is SSLHandshakeException -> CoreApplication.coreApp.toast(R.string.http_ssl_exception)
            //其他异常
            else -> CoreApplication.coreApp.toast(R.string.http_net_exception)
        }
        throwable.printStackTrace()
    }
}